home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / doc.lha / documentation / manual / structure.mss < prev    next >
Text File  |  1987-06-30  |  9KB  |  223 lines

  1. @part[STRUCTURE, root "TMAN.MSS"]    @Comment{-*-System:TMAN-*-}
  2. @chap[Structures]
  3.  
  4.  
  5. A structure corresponds to what is called a record in some other
  6. languages: a data structure with a fixed set of named fields or slots,
  7. known in @tau[] as @i[components].  For example, one might want an
  8. @qu"employee" structure, with slots for the employee's name, age, and
  9. salary.  A structure, then, is an organized set of @i[locations], places
  10. to store things.
  11. @index[Structures]
  12.  
  13. @dc{ Compare with use of @tc[LAMBDA] and @tc[OBJECT]. }
  14.  
  15.  
  16. @section[Terminology]
  17.  
  18. Every structure has an associated an object called a @iixs[structure
  19. type] (sometimes abbreviated @iix[stype], pronounced @qu"ess type").
  20. Structure types are created by the procedure @tc[MAKE-STYPE] (see below)
  21. and by the special form @tc[DEFINE-STRUCTURE-TYPE]
  22.  
  23. Given a structure type @i[S], one can access several things of interest:
  24.  
  25. @begin[Description]
  26. Constructor@\a procedure that will create uninitialized structures of
  27.   type @i[S] (e.g., the structure for a particular employee).
  28.  
  29. Predicator@\a procedure (a type predicate) that returns true if its
  30.   argument is a structure of type @i[S].
  31.  
  32. Component selectors@\procedures that access the various components of
  33.   structures of type @i[S] (one selector per component).
  34.  
  35. Handler@\the mechanism for specifying how a set of generic operations
  36.   will behave when applied to structures of type @i[S].
  37. @end[Description]
  38.  
  39. @dc{ mention the master copy also. }
  40.  
  41. @section[Defining structure types]
  42.  
  43. The @tc[DEFINE-STRUCTURE-TYPE] macro @dc{?}
  44. is a convenient way to define a new structure type and its
  45. associated constructor, predicator, and component selectors.
  46. @tc[DEFINE-STRUCTURE-TYPE] is a macro defined in terms of @tc[MAKE-STYPE] and
  47. the other low-level routines described in the next section.  One may
  48. want to define one's own interface to these routines, one that
  49. initializes the components when a new instance is constructed, for
  50. example.
  51.  
  52. @AnEquivE[Tfn="DEFINE-STRUCTURE-TYPE",Efn="DEFSTRUCT"]
  53. @AnEquivE[Tfn="DEFINE-STRUCTURE-TYPE",Efn="DEFVST"]
  54. @AnEquivE[Tfn="DEFINE-STRUCTURE-TYPE",Efn="RECORD-TYPE"]
  55. @info[NOTES="Special form"]
  56. @desc[(DEFINE-STRUCTURE-TYPE @i[typename . componentnames]) @yl[] @i[stype]]
  57. Creates a structure type (using @tc[MAKE-STYPE]),
  58. and defines a number of variables:
  59. @begin[Itemize]
  60. @i[typename]@tc[-STYPE] is defined to be the structure type.
  61.  
  62. @tc[MAKE-]@i[typename] is defined to be the constructor.
  63.  
  64. @i[typename]@tc[?] is defined to be the predicator.
  65.  
  66. @i[typename]@tc[-]@i[componentname] is defined to be a component selector, for
  67. each of the @i[componentnames].
  68. @end[Itemize]
  69. For example,
  70. @begin[ProgramExample]
  71. (DEFINE-STRUCTURE-TYPE EMPLOYEE NAME AGE SALARY)
  72. @end[ProgramExample]
  73. defines
  74. @begin[Itemize]
  75. @tc[EMPLOYEE-STYPE], a variable whose value is
  76. the structure type whose name is @tc[EMPLOYEE].
  77.  
  78. @tc[(MAKE-EMPLOYEE)], a procedure that creates uninitialized
  79. @tc[EMPLOYEE]-structures.
  80.  
  81. @tc[(EMPLOYEE? @i[object])], a type predicate.
  82.  
  83. @tc[(EMPLOYEE-NAME @i[employee])
  84. @*(EMPLOYEE-AGE @i[employee])
  85. @*(EMPLOYEE-SALARY @i[employee])], selectors that access the components of
  86. @tc[EMPLOYEE]-structures.
  87.  
  88. @end[Itemize]
  89. @EndDesc[DEFINE-STRUCTURE-TYPE]
  90.  
  91. @section[Manipulating structure types]
  92.  
  93. @desc[(MAKE-STYPE @i[typename componentnames]) @yl[] @i[stype]]
  94. Returns a new structure type.  The @i[typename], which should be a
  95. symbol, is used for printing and identification purposes only.
  96. @i[Componentnames] should be a list of symbols.  The @i[componentnames]
  97. correspond to the components that instances of the structure will have.
  98.  
  99. For example:
  100. @begin[ProgramExample]
  101. (MAKE-STYPE 'EMPLOYEE '(NAME AGE SALARY))  @ev[]  #{Stype EMPLOYEE}
  102. @end[ProgramExample]
  103. This creates a structure type, identified as @tc[EMPLOYEE],
  104. whose instances have components @tc[NAME], @tc[AGE], and @tc[SALARY].
  105. The object returned, a structure type, is appropriate as an argument to other
  106. routines described in this section.
  107. @EndDesc[MAKE-STYPE]
  108.  
  109. @desc[(STYPE-ID @i[stype]) @yl[] @i[typename]]
  110. Returns the type identifier for @i[stype].
  111. @EndDesc[STYPE-ID]
  112.  
  113. @desc[(STYPE-CONSTRUCTOR @i[stype]) @yl[] @i[procedure]]
  114. Given a structure type @i[stype], returns the procedure which will
  115. instantiate (i.e., create instances of) the structure type.  The
  116. constructor procedure takes no arguments and creates a structure with
  117. uninitialized components.
  118.  
  119. Note that the constructor procedure is created at the time the structure type
  120. is created, not at the time that @tc[STYPE-CONSTRUCTOR] is called.
  121. @begin[ProgramExample]
  122. ((STYPE-CONSTRUCTOR @i[stype]))  @ce[]  (COPY-STRUCTURE (STYPE-MASTER @i[stype]))
  123. @end[ProgramExample]
  124. @EndDesc[STYPE-CONSTRUCTOR]
  125.  
  126. @desc[(STYPE-MASTER @i[stype]) @yl[] @i[structure]]
  127. Returns the @qu"master copy" from which all structures created by the
  128. structure type's constructor-procedure are made.
  129. This can be convenient for establishing default values for components in
  130. newly created structures.  The way to do this is by storing into the
  131. master copy the desired default values; these values will be copied
  132. into all new instances of the structure type.  For example,
  133. @ProgramExample[
  134. (SET (EMPLOYEE-SALARY (STYPE-MASTER EMPLOYEE-STYPE)) 12000)
  135. (EMPLOYEE-SALARY (MAKE-EMPLOYEE))  @ev[]  12000
  136. ]
  137. @EndDesc[STYPE-MASTER]
  138.  
  139. @desc[(STYPE-PREDICATOR @i[stype]) @yl[] @i[procedure]]
  140. Given a structure type @i[stype], returns the procedure (a type predicate)
  141. which will predicate
  142. membership in the structure type.  This procedure takes one
  143. argument and returns true if that argument is an instance of the structure type.
  144. @dc{(i.e., was created using the structure type's @tc[STYPE-CONSTRUCTOR]).}
  145.  
  146. Note that the predicator procedure is created at the time the structure type
  147. is created, not at the time that @tc[STYPE-PREDICATOR] is called.
  148. @EndDesc[STYPE-PREDICATOR]
  149.  
  150. @desc[(STYPE-SELECTOR @i[stype componentname]) @yl[] @i[procedure]]
  151. Given a structure type @i[stype], returns the procedure which will
  152. select the component of the structure type's instances identified by
  153. @i[componentname].  The selector procedure takes one argument, which
  154. must be a structure whose structure type is @i[stype], and returns the
  155. appropriate component of the instance.
  156.  
  157. Such selector procedures support @tc[SETTER] operations, which is to say
  158. that one may alter components of structures by using @tc[SET] or related
  159. special forms.
  160.  
  161. The effect of selecting a structure component that has not been
  162. previously set, or initialized from the master structure
  163. (see @tc[STYPE-MASTER], above), is undefined.
  164.  
  165. Note that the selector procedures are created at the time the structure type
  166. is created, not at the time that @tc[STYPE-SELECTOR] is invoked.
  167. @EndDesc[STYPE-SELECTOR]
  168.  
  169. @desc[(STYPE-SELECTORS @i[stype]) @yl[] @i[list of procedures]]
  170. Given a structure type @i[stype], returns a list of its selector procedures.
  171. @EndDesc[STYPE-SELECTORS]
  172.  
  173. @desc[(SELECTOR-ID @i[selector]) @yl[] @i[identifier]]
  174. Given a selector procedure, e.g., as returned by @tc[STYPE-SELECTOR],
  175. returns its corresponding componentname.
  176. @EndDesc[SELECTOR-ID]
  177.  
  178. @info[NOTES="Settable"]
  179. @desc[(STYPE-HANDLER @i[stype]) @yl[] @i[handler]]
  180. Given a structure type @i[stype], accesses the handler to be used
  181. for generic operations applied to instances of the structure type.
  182. For a newly created structure type, this handler handles
  183. no operations, but one may set the handler to be any handler at all.
  184. @dc{ Give example. }
  185. @EndDesc[STYPE-HANDLER]
  186.  
  187. @section[Manipulating structures]
  188.  
  189. The primary means for manipulating structures is to call
  190. their selector procedures and their setters.  For example, in the context
  191. of the examples above, if @tc[EMPLOYEE-23] is an @tc[EMPLOYEE]-structure,
  192. then @tc[EMPLOYEE-23]'s @tc[NAME] component may be retrieved
  193. by evaluating
  194. @ProgramExample[
  195. (EMPLOYEE-NAME EMPLOYEE-23)
  196. ]
  197. and this value may be set by evaluating
  198. @ProgramExample[
  199. (SET (EMPLOYEE-NAME EMPLOYEE-23) 'FRED)
  200. ]
  201.  
  202. This works because the selector procedures for @tc[EMPLOYEE-STYPE]
  203. handle the generic operation called @tc[SETTER].
  204. Structures may also be manipulated using other generic operations.
  205. @dc{ as explained in the next section?? }
  206.  
  207. @info[Notes="Type predicate"]
  208. @desc[(STRUCTURE? @i[object]) @yl[] @i[boolean]]
  209. Returns true if @i[object] is a structure.
  210. @EndDesc[STRUCTURE?]
  211.  
  212. @desc[(COPY-STRUCTURE @i[structure]) @yl[] @i[structure]]
  213. Makes a copy of @i[structure].  The value returned is a new object
  214. of the same structure type as @i[structure] whose components
  215. are the same (in the @tc[EQ?] sense) as @i[structure]'s.
  216. @EndDesc[COPY-STRUCTURE]
  217.  
  218. @desc[(COPY-STRUCTURE! @i[destination source]) @yl[] @i[structure]]
  219. Copies the components of @i[source] into @i[destination], which
  220. is returned.
  221. @i[Source] and @i[destination] must be structures of the same type.
  222. @EndDesc[COPY-STRUCTURE!]
  223.